package com.hp.ssmp.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hp.ssmp.entity.TGoodsCategory;
import com.hp.ssmp.entity.TUser;
import com.hp.ssmp.entity.dto.TGoodsCategoryDto;
import com.hp.ssmp.service.CategoryService;
import com.hp.ssmp.service.UserService;
import com.hp.ssmp.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Resource
    private CategoryService categoryrService;

    /**
     * 获取分类列表
     * @return
     */
    @GetMapping("/list")
    public R<List<TGoodsCategoryDto>> getList() {
        List<TGoodsCategoryDto> list = categoryrService.getList();
        return R.success(list);
    }

    /**
     * 获取level小于参数的分类
     * @param level
     * @return
     */
    @GetMapping("/levelList")
    public R<List<TGoodsCategory>> getLevelList(Long level) {
        List<TGoodsCategory> list = categoryrService.getLevelList(level);
        return R.success(list);
    }
    @PostMapping("/update")
    public R update(TGoodsCategory category) {
        boolean result = categoryrService.updateById(category);
        if (result) {
            return R.success();
        } else {
            return R.error();
        }
    }
    @PostMapping("/add")
    public R add(TGoodsCategory category) {
        boolean result = categoryrService.save(category);

        if (result) {
            return R.success();
        } else {
            return R.error();
        }
    }
    @GetMapping("/del")
    public R del(Long categoryid) {
        boolean result = categoryrService.removeById(categoryid);
        if (result) {
            return R.success();
        } else {
            return R.error();
        }
    }
}
